import { AbstractModel } from "../types";
import { Comparable } from "../types/comparable";
import {
  numberToHash,
  stringToHash,
  getClassName,
  formatToString
} from "../utils";
/**
 * 模型：人,
 * 属性：姓名，年龄,
 * 继承接口：比较接口
 */
export class Person extends AbstractModel implements Comparable<Person> {
  static common(data: number): Person {
    return new Person(data + "", data);
  }
  constructor(private _name: string, private _age: number) {
    super();
  }
  get age(): number {
    return this._age;
  }
  get name(): string {
    return this._name;
  }
  get hash(): number {
    return numberToHash(this._age) * 31 + stringToHash(this._name);
  }
  equals(obj: unknown): boolean {
    if (obj === this) return true;
    if (obj === null || getClassName(this) !== getClassName(obj)) return false;
    return (
      (obj as Person).age === this.age && (obj as Person).name === this.name
    );
  }
  compareTo(obj: Person): number {
    return this.age - obj.age;
  }
  toString(): string {
    return formatToString("Person", ["name", this.name], ["age", this.age]);
  }
}
